package com.aim;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import com.aim.config.SpringRootConfig;
import com.aim.config.SpringWebConfig;
import com.aim.util.WebUtil;

/**
 * @AUTO 启动入口
 * @FILE LaunchApplication.java
 * @DATE 2021年4月13日 下午12:28:49
 * @Author AIM
 * @Version
 */
public class LaunchJettyApp {

	private static final Map<String, String> param = new HashMap<String, String>();

	public static void main(String[] args) throws Exception {
		if (args.length != 0) {
			for (String arg : args) {
				System.out.println(arg);
				if (!WebUtil.isEmpty(arg) && arg.contains("=")) {
					String[] t = arg.trim().split("=");
					param.put(t[0], t[1]);
				}
			}
		}
		// 配置服务,创建服务上下文
		ServletContextHandler handler = new ServletContextHandler();
		handler.setDisplayName("customDefaultServlet");
		// ip:port/项目路径/API请求路径
		handler.setContextPath("/");
		// 注册所注释的配置类
		AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
		applicationContext.register(SpringWebConfig.class);
		applicationContext.register(SpringRootConfig.class);
		// 相当于web.xml中配置的ContextLoaderListener
		handler.addEventListener(new ContextLoaderListener(applicationContext));
		// springMVC拦截规则 相当于web.xml中配置的DispatcherServlet
		handler.addServlet(new ServletHolder(new DispatcherServlet(applicationContext)), "/*");

		Server server = new Server(80);// 创建jettyWeb容器
		server.setHandler(handler);
		server.setStopAtShutdown(true);// 在退出程序是关闭服务
		try {
			server.start();
			server.join();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
